package com.gxa.dao.impl;

import com.gxa.common.Constants;
import com.gxa.dao.EmpDao;
import com.gxa.dbutils.JdbcBuild;
import com.gxa.pojo.Department;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;

import java.sql.SQLException;
import java.util.List;

public class EmpDaoImpl extends JdbcBuild implements EmpDao {

    @Override
    public List<User> queryemplist(Integer p1, Integer p2) throws SQLException {
        String sql = "select * from user where del_status=? limit ?,?";
        Object[] parmar={Constants.Del_STATUS_OK,p1,p2};
        return super.queryListEntity(User.class,sql,parmar);
    }

    @Override
    public List<User> queryUserList(Integer p1, Integer p2, String username) throws SQLException {
        String sql = "select * from user where username=? and del_status=? limit ?,?";
        Object[] params = {username,Constants.Del_STATUS_OK,p1,p2};
        return super.queryListEntity(User.class,sql,params);
    }

    @Override
    public Department getDepartment(Integer department_id) throws SQLException {
        String sql = "select * from department where id = ?";
        return super.queryEntity(Department.class,sql,department_id);
    }

    @Override
    public Role getRole(Integer role_id) throws SQLException {
        String sql = "select * from role where id = ?";
        return super.queryEntity(Role.class, sql, role_id);
    }

    @Override
    public Integer countUser() throws SQLException {
        String sql = "select count(*) from user where del_status = ?";
        return super.findAllForInt(sql,Constants.Del_STATUS_OK);
    }

    @Override
    public Integer countUser(String username) throws SQLException {
        String sql = "select Count(*) from user where del_status =? and username=?";
        Object[] params = {Constants.Del_STATUS_OK,username};
        return super.findAllForInt(sql,params);
    }

    @Override
    public Integer addEmp(User user) throws SQLException {
        String sql = "insert into user(username,sex,update_time,status,mark,role_id,name_num,del_status,role_name,department_name,password,tel) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {user.getUsername(),user.getSex(),user.getUpdate_time(),user.getStatus(),user.getMark(),
                user.getRole_id(),user.getName_num(),Constants.Del_STATUS_OK,user.getRole_name(),user.getDepartment_name(),user.getInitialpassword(),user.getTel()};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Role searchRole(User user) throws SQLException {
        String sql = "select id from role where role_name=? and department_id=? and del_status=?";
        Object[] params = {user.getRole_name(),user.getDepartment_id(),Constants.Del_STATUS_OK};
        return super.queryEntity(Role.class,sql,params);
    }

    @Override
    public Integer delUser(String id) throws SQLException {
        String sql = "update user set del_status=? where id=?";
        Object[] params = {Constants.Del_STATUS_DEL,id};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer updateUser(User user) throws SQLException {
        String sql = "update user set username=?,name_num=?,mark=?,update_time=?,sex=?,status=?,role_id=?,role_name=?,department_name=? where id=?";
        Object[] params = {user.getUsername(),user.getName_num(),user.getMark(),user.getUpdate_time(),user.getSex(),user.getStatus(),user.getRole_id(),user.getRole_name(),user.getDepartment_name(),user.getId()};
        System.out.println(user.getId());
        System.out.println(user.getRole_id());
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer enableUser(String id) throws SQLException {
        String sql = "update user set status=? where id=?";
        Object[] params = {Constants.Del_STATUS_OK,id};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer disableUser(String id) throws SQLException {
        String sql = "update user set status=? where id=?";
        Object[] params = {Constants.Del_STATUS_DEL,id};
        return super.executeUpdate(sql,params);
    }

    @Override
    public User statusUser(String id) throws SQLException {
        String sql = "select status from user where id=?";
        return super.queryEntity(User.class,sql,id);
    }

    @Override
    public Department searchDepartName(Integer id) throws SQLException {
        String sql = "select * from department where id=?";
        return super.queryEntity(Department.class,sql,id);
    }

    @Override
    public Integer editUserInfo(String username, String name_num, String oldpassword, String newpassword) throws SQLException {
        String sql = "update user set username=?,password=? where name_num=? and password=?";
        Object[] p = {username,newpassword,name_num,oldpassword};
        return super.executeUpdate(sql,p);
    }


}
